/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sys;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;   
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author sochy
 */
public class DB implements IDb{
    public Connection connection = null; // obj of class 'sql.Connetion' used to connect database from server site
    public Statement st;
    public ResultSet rs;                 // obj of class 'sql.ResultSet'  , ResultSet is abstract method that call from class IDb , that Class DB implements from IDB
    public ArrayList<Object> data;       // ArrayList used to store data from resultSet  (mean that, ResultsSet got data from sever --> arrayList get data from ResultSet --> move data to table ) 
    public DB(){  // constractor , why we create connection in constractor ? cus the begining of proscess in class is constractor 
        
        try {
            Class.forName("com.mysql.jdbc.Driver"); // includ driver 
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/javapos", "root", "");  // conection
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);   // 
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public ResultSet select(String sql) {

        try {
            // create the java statement
            st = connection.createStatement();
            // execute the query, and get a java resultset
            rs= st.executeQuery(sql);
          //  JOptionPane.showMessageDialog(null, rs);
           
           
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        
       
     return rs;   
    }

    @Override
    public boolean insert(String sql) {
        try {
            // create the java statement
            st = connection.createStatement();
            // execute the query, and get a java resultset
            st.execute(sql);
            
        } catch (SQLException ex) {
            //Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
            return false;
            
        }finally{
            return true;
        }
    }

    @Override
    public boolean update(String sql) {
        try {
            // create the java statement
            st = connection.createStatement();
            // execute the query, and get a java resultset
            st.execute(sql);
            
        } catch (SQLException ex) {
            return false;
//            JOptionPane.showMessageDialog(null, "Item cannot update, please try again");
//            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            return true;
        }
    }

    @Override
    public boolean delete(String sql) {
        try {
            // create the java statement
            st = connection.createStatement();
            // execute the query, and get a java resultset
            st.execute(sql);
            
        } catch (SQLException ex) {
            return false;
        }
        finally{
            return true;
        }
        
    }
    
    public void close(){
        try {
            this.st.close();
        } catch (SQLException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

}
